﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;
using System.IO;
namespace LaKuaShop_TestDemo.Page
{
    /// <summary>
    /// ProductDetailPage.xaml 的交互逻辑
    /// </summary>
    public partial class ProductDetailPage : System.Windows.Controls.Page
    {
        string Pid;
        ProductInfo product;
        public ProductDetailPage(string pid)
        {
            InitializeComponent();
            Pid = pid;
            IniProduct();
        }


        void IniProduct()
        {
            BLL.Product Product = new BLL.Product();
            product = Product.ShowProductInfo(Pid);
            Pname.Text = product.Pname;
            Pname.IsReadOnly = true;
            Pintro.AppendText(product.Pintro);
            Pprice.Text = "￥"+product.Pprice.ToString();
            Pprice.IsReadOnly = true;
            Psale.IsReadOnly = true;
            Psale.Text = "￥"+product.Psale.ToString();
            Pintro.IsReadOnly = true;          
            byte[] img = Product.ShowProductImg(product.Pid);
            
            if (img != null)//每个格子加上图片背景
            {
                MemoryStream stream = new MemoryStream(img);
                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();//初始化
                bmp.StreamSource = stream;//设置源
                bmp.EndInit();//初始化结束
                IMG.Source = bmp;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)//点击“-”按钮
        {
            int Num = Convert.ToInt32(num.Text);
            if (Num >1)
            {
                Num--;
                num.Text = Num.ToString();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)//点击“+”按钮
        {
            int Num = Convert.ToInt32(num.Text);
            if (Num < 99)
            {
                Num++;
                num.Text = Num.ToString();
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)//点击"立即购买"按钮
        {

            int Num = Convert.ToInt32(num.Text);
            if (Num > product.Pnum)
            {
                MessageBox.Show("库存不足~请耐心等待管理员补货哦♥");
                return;
            }
            //为了和购物车界面进入结算保持一致，将立即购买的物品也暂时加入购物车
            BLL.BuyCart BuyCart = new BLL.BuyCart();
            BuyCart.AddOneBuyCart(Common.User.account, Pid, Num);
            List<string> Pids = new List<string>();
            List<int> Nums = new List<int>();

            Pids.Add(Pid);
            Nums.Add(Convert.ToInt32(num.Text));
            BuyCartInfo buyCart = new BuyCartInfo(Common.User.account, Pids, Nums);
            NavigationService.GetNavigationService(this).Navigate(new OrderPage(buyCart));
        }

        //加入收藏夹
        private void AddToCollect(object sender, RoutedEventArgs e)
        {
            BLL.Collect Clt = new BLL.Collect();
            if (Clt.AddOneCollection(Common.User.account, Pid) == 0)
            {
                MessageBox.Show("该商品已成功加入收藏夹，快来看看吧~");
            }
            else
            {
                MessageBox.Show("该商品已在你的收藏夹中了哦");
            }
        }

        //加入购物车
        private void AddToCart(object sender, RoutedEventArgs e)
        {
            BLL.BuyCart buyCart = new BLL.BuyCart();
            int Num = Convert.ToInt32(num.Text);
            if (buyCart.AddOneBuyCart(Common.User.account,Pid,Num) == 0)
            {
                MessageBox.Show("商品已成功加入购物车~快来看看吧~");
            }
            else
            {
                MessageBox.Show("系统好像出问题了QAQ");
            }
        }
    }
}
